/*
* $Id: TableCelRow.cpp 14 2010-01-13 13:07:55Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include <tinyxml.h>
#include "TableCelRow.h"
#include "TableCel.h"

CTableCelRow::CTableCelRow(int nId, int nType)
: m_nId(nId), m_pBack(NULL), m_pNext(NULL), m_nType(nType){
	GetXmlCy();
}

CTableCelRow::CTableCelRow(const CTableCelRow& row){
	*this = row;
}
CTableCelRow::~CTableCelRow(){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++)
		delete *it;
	SetXmlCy();
}

bool CTableCelRow::Paint(CDC* dc, CRect rect){
	if(m_posY > rect.bottom)
		return false;
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		(*it)->SetYPos(m_posY);
		(*it)->Init();
		(*it)->Paint(dc);
	}
	return true;
}

void CTableCelRow::AddCel(tableHeader* pHeader, const std::tstring& strValue){
	CTableCel* tableCel = new CTableCel(strValue, pHeader);
	tableCel->SetCy(GetCY());
	m_aCels.push_back(tableCel);
}

void CTableCelRow::operator = (const CTableCelRow& row){
	m_aCels = row.m_aCels;
	m_posY = row.m_posY;
	m_pBack = row.m_pBack;
	m_pNext = row.m_pNext;
	if(m_pBack)
		m_pBack->m_pNext = this;
	if(m_pNext)
		m_pNext->m_pBack = this;
}

void CTableCelRow::SetPosY(int posY){
	m_posY = posY;
}
void CTableCelRow::SetCY(int cy){
	m_nCy = cy;
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++)
		(*it)->SetCy(cy);
}

int CTableCelRow::GetCY(){
	return m_nCy;
}
int CTableCelRow::GetId(){
	return m_nId;
}

void CTableCelRow::Select(bool bSel){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		if(bSel)
			(*it)->SetColor(RGB(200, 200, 200));
		else
			(*it)->SetColor(RGB(255, 255, 255));
	}
}

int CTableCelRow::GetYPos(){
	return m_posY;
}

std::tstring CTableCelRow::GetValueColumn(int nId){
	for(std::vector<CTableCel*>::iterator it = m_aCels.begin(); it != m_aCels.end(); it++){
		if(((tableHeader*)((*it)->m_pHeader))->m_nId == nId)
			return (*it)->m_strValue;
	}
	return _T("");
}

bool CTableCelRow::IsSetCursor(CPoint pos){
	return m_posY + GetCY() >= pos.y && m_posY + GetCY() - 3 < pos.y;
}

void CTableCelRow::GetXmlCy(){
	m_nCy = (CItemTable::sizeFont + 5) * 2;
	TiXmlDocument xml("table.xml");
	if(xml.LoadFile(TIXML_ENCODING_UTF8)){
		TiXmlElement* root = xml.RootElement();
		if(!root)
			return;
		for(TiXmlElement* element = root->FirstChildElement("cell_row"); element; element = element->NextSiblingElement("cell_row")){
			int id = 0, type = 0;
			element->Attribute("id", &id);
			element->Attribute("type", &type);
			if(id == m_nId && type == m_nType){
				element->Attribute("size", &m_nCy);
				break;
			}
		}
	}
}

void CTableCelRow::SetXmlCy(){
	TiXmlDocument xml("table.xml");
	if(!xml.LoadFile(TIXML_ENCODING_UTF8)){
		TiXmlDeclaration decl("1.0", "UTF-8", ""); 
		xml.InsertEndChild(decl);
		TiXmlElement root("table");
		xml.InsertEndChild(root);
	}
	TiXmlElement* root = xml.RootElement();
	TiXmlElement* element;
	for(element = root->FirstChildElement("cell_row"); element; element = element->NextSiblingElement("cell_row")){
		int id = 0, type = 0;
		element->Attribute("id", &id);
		element->Attribute("type", &type);
		if(id == m_nId && type == m_nType){
			break;
		}
	}
	if(!element){
		TiXmlElement elem("cell_row");
		elem.SetAttribute("id", m_nId);
		elem.SetAttribute("type", m_nType);
		element = root->InsertEndChild(elem)->ToElement();
	}
	element->SetAttribute("size", m_nCy);
	xml.SaveFile();
}